﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.Other
 * 唯一标识：7cf7b75b-081a-4821-8178-ad24087bc9e3
 * 文件名：Image
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/7/24 17:10:15
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 图片控件
/// </summary>
public sealed class Image : ControlBase
{
    private BlogOptions Options { get; set; }

    /// <summary>
    /// 构造函数
    /// </summary>
    internal Image()
    {
        var option = App.Service.GetRequiredService<IBlogOption<BlogOptions>>();

        Options = option.GetValue();
    }

    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        Param par = new Param();

        if (FitType != ImageFitTypeEnum.Default) par.Add($"fit=\"{FitType.GetDescription()}\"");

        if (IsFromLocal)
        {
            if (Options.ApiUrl.EndsWith("/") && Url.StartsWith("/"))
                Url = Url.Substring(1);

            Url = $"{Options.ApiUrl}{Url}";
        }

        string style = $"style=\"width: {Width}px; height: {Height}px\"";

        if (!string.IsNullOrEmpty(CustomStyle))
            style = CustomStyle;

        sb.AppendLine($"<el-image {style} :src=\"'{Url}'\" {par}/>");

        return sb.ToString();
    }

    /// <summary>
    /// 是否来自本地
    /// </summary>
    internal bool IsFromLocal { get; set; }

    /// <summary>
    /// 图片地址
    /// </summary>
    internal string Url { get; set; }

    internal int Width { get; set; }

    internal int Height { get; set; }

    internal ImageFitTypeEnum FitType { get; set; }

    /// <summary>
    /// 自定义样式
    /// </summary>
    internal string CustomStyle { get; set; }
}

public static partial class App
{
    /// <summary>
    /// 创建图片控件
    /// </summary>
    /// <param name="isFromLocal">图片是否来源本地</param>
    /// <param name="url"></param>
    /// <param name="width">图片宽</param>
    /// <param name="height">图片高</param>
    /// <param name="fitType"></param>
    /// <returns></returns>
    public static Image CreateImage(bool isFromLocal, string url, int width = 100, int height = 100, ImageFitTypeEnum fitType = ImageFitTypeEnum.Default)
    {
        if (width <= 0) width = 10;
        if (height <= 0) height = 10;

        Image image = new Image()
        {
            Id = GenerateId(),
            Title = "图片",
            IsFromLocal = isFromLocal,
            Url = url,
            Width = width,
            Height = height,
            FitType = fitType
        };

        return image;
    }

    /// <summary>
    /// 创建图片控件
    /// </summary>
    /// <param name="isFromLocal">图片是否来源本地</param>
    /// <param name="url">路径</param>
    /// <param name="customStyle">自定义样式</param>
    /// <param name="fitType"></param>
    /// <returns></returns>
    public static Image CreateImage(bool isFromLocal, string url,string customStyle, ImageFitTypeEnum fitType = ImageFitTypeEnum.Default)
    {
        Image image = new Image()
        {
            Id = GenerateId(),
            Title = "图片",
            IsFromLocal = isFromLocal,
            Url = url,
            CustomStyle = customStyle,
            FitType = fitType
        };

        return image;
    }
}

/// <summary>
/// 图片适应容器框类型
/// </summary>
public enum ImageFitTypeEnum
{
    /// <summary>
    /// 默认
    /// </summary>
    [Description("")]
    Default,

    /// <summary>
    ///
    /// </summary>
    [Description("fill")]
    Fill,

    /// <summary>
    ///
    /// </summary>
    [Description("contain")]
    Contain,

    /// <summary>
    ///
    /// </summary>
    [Description("cover")]
    Cover,

    /// <summary>
    ///
    /// </summary>
    [Description("none")]
    None,

    /// <summary>
    ///
    /// </summary>
    [Description("scale-down")]
    ScaleDown,
}